//
//  DemoController.swift
//  DemoUI
//
//  Created by 白瑾浩 on 2022/2/22.
//

import UIKit

class BackgroundController: UIViewController {
    
    var uiButton:UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        self.title = "背景图片设置"
        
        let uiview1 = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
        self.view.addSubview( uiview1 )
        //设置当前视图背景
        //uiview1.backgroundColor = UIColor(patternImage: UIImage(named:"bg")!)
        
        //        uiview1.backgroundColor = UIColor(patternImage: UIImage(named:"bg")!.reSizeImage(reSize: CGSize(width: 300, height: 300)))
        
        
        
        //设置当前视图背景
        //uiview1.layer.contents = UIImage(named:"bg")?.cgImage
        
        
        
        //创建一个用于显示背景图片的imageView
        let backgroundImage = UIImageView(frame: uiview1.bounds)
        backgroundImage.image = UIImage(named: "bg")
        backgroundImage.contentMode = .scaleAspectFill//等比缩放填充（图片可能有部分显示不全）
        backgroundImage.clipsToBounds = true //设置超出边界不显示
        //将背景图片imageView插入到当前视图中
        uiview1.insertSubview(backgroundImage, at: 0)
        
        
        
        //图片拉伸
        let uiview2 = UIView(frame: CGRect(x: 0, y: 300, width: 300, height: 64))
        uiview2.backgroundColor = .blue
        self.view.addSubview( uiview2 )
        let backgroundImage2 = UIImageView(frame: uiview2.bounds)
        backgroundImage2.backgroundColor = .red
        var image = UIImage(named: "对话框")!
    
        image = image.resizableImage(withCapInsets: UIEdgeInsets(top: 20, left: 30 , bottom: 30, right: 15 ), resizingMode: UIImage.ResizingMode.stretch)
        //image = image.stretchableImage(withLeftCapWidth: 300/2, topCapHeight: 40/2)
        backgroundImage2.image = image
        uiview2.insertSubview(backgroundImage2, at: 0)
        
        
        
        
        uiButton = UIButton( buttonType: UIButton.ButtonType.custom, title: "显示", titleColor: UIColor.black, titleFont: UIFont.systemFont(ofSize: 20 ), titleEdgeInsets: UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0), textAlignment: NSTextAlignment.left, contentHorizontalAlignment: UIControl.ContentHorizontalAlignment.left, buttonWidth: 100, buttonHeight: 40)
        uiButton.frame.origin = CGPoint(x: 20, y: 400)
        self.view.addSubview( uiButton )
        uiButton.ischeckBox(isCheck: true)
        uiButton.addTarget(self, action: #selector(action(uiButton:)), for: UIControl.Event.touchUpInside)
        
        
        
        
        
    }
    //切换状态
    @objc func action(uiButton:UIButton){
        uiButton.ischeckBox(isCheck: !uiButton.isSelected )
    }
    
}

extension UIButton{
    //构造UIBUTTON
    convenience init(buttonType:ButtonType = UIButton.ButtonType.system,title: String = "button",
                     titleColor: UIColor = .black,
                     titleFont: UIFont = UIFont.systemFont(ofSize: 20),
                     titleEdgeInsets: UIEdgeInsets = .zero,
                     textAlignment : NSTextAlignment = .left,
                     contentHorizontalAlignment : ContentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left,
                     buttonWidth:Int = Int(UIScreen.main.bounds.width),
                     buttonHeight:Int = 60,
                     imgae:UIImage? = nil,
                     target: Any? = nil,
                     action: Selector? = nil,
                     tintColor:UIColor? = nil) {
        self.init(type: buttonType )
        self.setTitle( title, for: UIControl.State.normal)
        self.setTitleColor( titleColor, for: UIControl.State.normal )
        self.titleLabel?.font = titleFont
        self.titleLabel?.textAlignment = textAlignment
        self.contentHorizontalAlignment = contentHorizontalAlignment
        if imgae != nil {
            self.setImage( imgae, for: UIControl.State.normal)
        }
        self.titleEdgeInsets = titleEdgeInsets
        //或者
        //UIButton.Configuration.filled()  15.0以后
        self.backgroundColor = .white
        if tintColor != nil {
            self.tintColor = tintColor
        }
        self.frame = CGRect(x: 0, y: 0, width: buttonWidth, height: buttonHeight)
        
        if action != nil{
            self.addTarget(target!, action: action!, for: UIControl.Event.touchUpInside)
        }
        
    }
    //默认图片和传自己定义图片  默认图片我这里放在assets中
    func ischeckBox( isCheck:Bool = false, imgae:UIImage? = nil,imageEdgeInsets:UIEdgeInsets =  UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)  ){
        var  _image:UIImage
        if imgae == nil {
            _image = UIImage(named: (isCheck ? "checkbox-sel" : "checkbox-nor") )!.reSizeImage(reSize: CGSize(width: 30, height: 30))
        }else{
            _image = imgae!
        }
        
        self.setImage( _image, for: UIControl.State.normal)
        self.imageEdgeInsets = imageEdgeInsets//UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)//图片边距
        self.isSelected = isCheck
        
    }
}


extension  UIImage{
    /**
     *  重设图片大小
     */
    func  reSizeImage(reSize: CGSize,  scale : CGFloat = UIScreen.main.scale )-> UIImage  {
        //UIGraphicsBeginImageContext(reSize);
        UIGraphicsBeginImageContextWithOptions (reSize, false , scale)
        self.draw( in: CGRect(x:0, y:0, width:reSize.width, height:reSize.height) )
        let  reSizeImage: UIImage  =  UIGraphicsGetImageFromCurrentImageContext ()!
        UIGraphicsEndImageContext ()
        return  reSizeImage
    }
    //等比例缩放 根据高宽对比
    func  scaleImage(reSize: CGSize )-> UIImage  {
        
        let oldSize = self.size
        //目标高宽都大于原始高宽 则返回原图
        if reSize.height >= oldSize.height && reSize.width >= oldSize.width {
            return self
        }
        let hRatio = oldSize.height / reSize.height
        let wRatio = oldSize.width / reSize.width
        
        var desSize:CGSize
        //以高缩放
        if hRatio > wRatio{
            desSize =  CGSize(width:oldSize.width * reSize.height/oldSize.height  ,  height: reSize.height )
            //以宽缩放
        }else{
            desSize =  CGSize(width: reSize.width,  height: oldSize.height * reSize.width/oldSize.width  )
        }
        return  reSizeImage(reSize: desSize, scale: CGFloat(desSize.width)/CGFloat(desSize.height) )
    }
}
